package com.famid.flashcards;

import java.util.List;
import com.famid.flashcards.data.CardStack;
import com.famid.flashcards.data.CardStackDAOImpl;
import com.famid.flashcards.data.CardStackDAO;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ListStackActivity extends ListActivity{

	private static final String TAG = ListStackActivity.class.getName();
	private CardStackDAO stackDao;
	private List<CardStack> stacks;
	private boolean onContextMenuShow = false;
	private CardStack selected;
	private ListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cards_stuck_list);
		stackDao = new CardStackDAOImpl(this);
		stackDao.open();

		ListView lv = getListView();
		registerForContextMenu(lv);
	}
	
	private void init() {
		stacks = stackDao.getAllCardStacks();
		adapter = new ListAdapter(this, stacks);
		setListAdapter(adapter);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.stack_list_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.stack_add:
        	addStack();
            return true;
//        case R.id.menu_about:
//           	showAbout();
//            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	private void addStack() {
    	final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	
    	alert.setTitle(R.string.create_new_stack);
    	alert.setMessage(R.string.stack_create_name);
    	
    	// Set an EditText view to get user input 
    	final EditText input = new EditText(this);
    	alert.setView(input);
    	
    	alert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			Editable value = input.getText();
    			if(value.length() != 0) {
	    			CardStack cs = new CardStack(1, value.toString());
	    			cs = stackDao.createCardStack(cs);
	    			stacks.add(cs);
	    			adapter.notifyDataSetChanged();
	    			Log.d(TAG, "Created new stack: " + value+" id:"+cs.getId());
    			}
    			else {
    				Toast.makeText(ListStackActivity.this, R.string.stack_create_empty_name, Toast.LENGTH_LONG).show();
    			}
    		}
    	});
    	
    	alert.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			Log.d(TAG, "Canceled adding new stack.");
    		}
    	});
    	
    	alert.show();
	}
	
	private void renameStack() {
		final AlertDialog.Builder renameDialog = new AlertDialog.Builder(this);
    	
		renameDialog.setTitle(R.string.rename);
		renameDialog.setMessage(R.string.rename_dialog);
    	
    	// Set an EditText view to get user input 
    	final EditText input = new EditText(this);
    	renameDialog.setView(input);
    	
    	renameDialog.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			Editable value = input.getText();
    			if(value.length() != 0) {
//	    			CardStack cs = new CardStack(1, value.toString());
    				selected.setTitle(value.toString());
	    			stackDao.update(selected);
	    			Log.d(TAG, "Rename stack: " + value);
	    			adapter.notifyDataSetChanged();
    			}
    			else {
    				Toast.makeText(ListStackActivity.this, R.string.rename_stack_empty_name, Toast.LENGTH_LONG).show();
    			}
    		}
    	});
    	
    	renameDialog.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			Log.d(TAG, "Canceled rename stack.");
    		}
    	});
    	
    	renameDialog.show();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(!onContextMenuShow) {
			Intent mainIntent = new Intent(this, CardsActivity.class);
			Bundle bundle = new Bundle();
		    bundle.putLong("stackId", stacks.get(position).getId());
		    mainIntent.putExtras(bundle);
			this.startActivity(mainIntent);
		}
		onContextMenuShow = false;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		if (v.getId()==getListView().getId()) {
		    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		    selected = stacks.get(info.position);
		    menu.setHeaderTitle(selected.getTitle());
		    menu.add(Menu.NONE, 0, 0, R.string.rename);
		    menu.add(Menu.NONE, 1, 1, R.string.delete);
		    onContextMenuShow = true;
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId()) {
		case 0:
			renameStack();
			break;
		case 1:
			stackDao.deleteCardStack(selected);
			stacks.remove(selected);
			adapter.notifyDataSetChanged();					
			break;
		}
		
		return super.onContextItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		Log.d(TAG, "Resumed");
		stackDao.open();
		init();
		super.onResume();
	}

	@Override
	protected void onPause() {
		Log.d(TAG, "Paused");
		stackDao.close();
		super.onPause();
	}
}
